package code.cando;

/**
 * author : yeswater
 * date : 2023/12/8
 * <p>
 * 跳跃游戏
 * <p>
 * 11-13 - 11-24
 */

public class T055 {
    public static void main(String[] args) {
        int[] nums = {2,3,1,1,4};
        // int[] nums = {2, 0};
        // System.out.println(canJump(nums));
        System.out.println(func1(nums));
    }

    private static boolean flag = false;

    public static boolean canJump(int[] nums) {
        int length = nums.length;
        if (length <= 1) {
            return true;
        }
        func(nums, 0, nums[0], length - 1);
        return flag;
    }

    private static void func(int[] nums, int index, int maxIndex, int end) {
        if (maxIndex >= end) {
            flag = true;
            return;
        }
        for (int i = maxIndex; i >= index; i--) {
            if (nums[i] == 0) {
                break;
            }
            func(nums, index + 1, index + nums[i], end); // [i, nums[i]] 这一步可以跳的范围
            index -= 1;
        }
    }
    
    private static boolean func1(int[] nums) {
        int k = 0;
        for (int i = 0; i < nums.length; i++) {
            if(i > k) {
                return false;
            } else {
                k = Math.max(k, i + nums[i]);
            }
        }
        return true;
    }
}
